/**
 * 
 */
package com.mango.mstock.support.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.mango.common.util.DateUtil;
import com.mango.mstock.model.MyFavorite;
import com.mango.mstock.support.MStockGlobalVars;

/**
 * @author richard
 * 
 */
public class DBManager {

	private DBHelper helper;
	private SQLiteDatabase db;

	public DBManager() {
		helper = new DBHelper(MStockGlobalVars.getCurrentContext());
		db = helper.getWritableDatabase();
	}
	
	public void saveOrUpdate(MyFavorite favorite) {
		MyFavorite entity = findByCode(favorite.getCode());
		if (entity == null) {
			save(favorite);
		} else {
			update(favorite);
		}
	}

	private void save(MyFavorite favorite) {
		db.beginTransaction();
		try {
			db.execSQL("INSERT INTO myfavors(code, name, yesterdayprice, latestprice, updatedate) " +
					   "VALUES (?, ?, ?, ?, ?)",
					new Object[] { favorite.getCode(), favorite.getName(), 
						favorite.getYesterdayPrice(), favorite.getLatestPrice(), 
						DateUtil.toString(new Date(), DateUtil.DATE_TIME_FORMAT)});
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
	}

	private void update(MyFavorite favorite) {
		ContentValues cv = new ContentValues();
		cv.put("yesterdayprice", favorite.getYesterdayPrice());
		cv.put("latestprice", favorite.getLatestPrice());
		cv.put("updatedate", DateUtil.toString(new Date(), DateUtil.DATE_TIME_FORMAT));
		db.update("myfavors", cv, "code = ?", new String[] { favorite.getCode() });
	}

	public List<MyFavorite> findAll() {
		ArrayList<MyFavorite> myFavors = new ArrayList<MyFavorite>();
		Cursor c = db.rawQuery("SELECT * FROM myfavors", null);
		while (c.moveToNext()) {
			MyFavorite favorite = new MyFavorite();
			favorite.set_id(c.getInt(c.getColumnIndex("_id")));
			favorite.setCode(c.getString(c.getColumnIndex("code")));
			favorite.setName(c.getString(c.getColumnIndex("name")));
			favorite.setYesterdayPrice(c.getFloat(c.getColumnIndex("yesterdayprice")));
			favorite.setLatestPrice(c.getFloat(c.getColumnIndex("latestprice")));
			favorite.setUpdatedate(c.getString(c.getColumnIndex("updatedate")));
					
			myFavors.add(favorite);
		}
		c.close();
		return myFavors;
	}
	
	public MyFavorite findByCode(String code) {
		Cursor c = db.rawQuery("select * from myfavors where code=?",new String[]{code});
		MyFavorite favorite = null;
		if (c.moveToNext()) {
			favorite = new MyFavorite();
			favorite.set_id(c.getInt(c.getColumnIndex("_id")));
			favorite.setCode(c.getString(c.getColumnIndex("code")));
			favorite.setName(c.getString(c.getColumnIndex("name")));
			favorite.setYesterdayPrice(c.getFloat(c.getColumnIndex("yesterdayprice")));
			favorite.setLatestPrice(c.getFloat(c.getColumnIndex("latestprice")));
			favorite.setUpdatedate(c.getString(c.getColumnIndex("updatedate")));
		}
		
		c.close();
		return favorite;
		
	}

	public void closeDB() {
		db.close();
	}

}
